 <?php
#
# author: Phil Spagnolo
# (c)2009 South Hill Calvary Chapel
#
function userIsApple() {
	$agent = $_SERVER['HTTP_USER_AGENT'];	
	$var[0] = 'iPhone';
	$var[1] = 'iPad';
	$var[2] = 'iPod';
	$var[3] = 'Macintosh';
	$result = count($var);
		for ($i=0;$i<$result;$i++){  
			$ausg = stristr($agent, $var[$i]);  
			if(strlen($ausg)>0){
				return TRUE;
				exit;
			}
		}
}
function print_html_head($title, $meta_key, $meta_desc) 
{
	$now = time();
	$YEAR = date('Y',$now);
	printf("<!DOCTYPE html>\n");
	printf(" <!--\n");
	printf("#\n");
	printf("# (c)%s South Hill Calvary Chapel\n", $YEAR);
	printf("#\n");
	printf("-->\n");
	printf("<html xmlns='http://www.w3.org/1999/xhtml'>\n");
	printf("\n");
	printf("<head>\n");
	printf("<title>South Hill Calvary Chapel :: %s</title>\n", $title);
	printf("\n");
	printf("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>\n");
	printf("<meta name='keywords' content='South, Hill, Calvary, Chapel, Puyallup, Washington, Church, %s'/>\n", $meta_key);
	printf("<meta name='description' content='%s'/>\n", $meta_desc);
	printf("<meta name='content-type' content='text/html'/>\n");

	include_once("analyticstracking.php");

	printf("<link type='text/css' rel='stylesheet' href='${root}/js/galleryview-3.0b3/css/jquery.galleryview-3.0.css' />\n");
	printf("<link type='text/css' rel='stylesheet' href='${root}/js/superfish/css/superfish.css' />\n");
	printf("<link href='${root}/css/layout.css' rel='stylesheet' type='text/css'/>\n");
	printf("<link href='${root}/css/needs.css' rel='stylesheet' type='text/css'/>\n");
	printf("<link rel='shortcut icon' href='/images/favicon.ico' />\n");
	printf("\n");
}

function print_java_head($more) 
{
	?>
	<script type="text/javascript" src='/js/jquery-1.5.1.min.js'></script>
	<script type='text/javascript' src='/js/jquery.easing.1.3.js'></script>
	<script type='text/javascript' src='/js/jquery.timers-1.2.js'></script>
	<script type='text/javascript' src='/js/lendingLibrary.js'></script>
	<script type='text/javascript' src='/js/js.js'></script>
	<script type='text/javascript' src='/js/galleryview-3.0b3/js/jquery.galleryview-3.0.min.js'></script>
	<script type='text/javascript' src='/js/superfish/js/superfish.js'></script>
	<script type='text/javascript' src='/js/superfish/js/hoverIntent.js'></script>
	<script type='text/javascript' src='/js/superfish/js/supersubs.js'></script>
	<!--Google AJAX Feed API -->
	<script src='http://www.google.com/jsapi' type='text/javascript'></script>
	<script src='http://www.google.com/uds/solutions/slideshow/gfslideshow.js' type='text/javascript'></script>
	<script type="text/javascript">
		$(document).ready(function(){
			setTimeout(function(){
				var test = document.createElement("input");
				if (!("placeholder" in test)){
					$(".noPlaceholder").css("display", "inline");
				}
			}, 500);
		});
	</script>
	<script type='text/javascript'>
	//SUPERSUBS
	jQuery(function(){
	jQuery('ul.sf-menu').supersubs({
	minWidth: 10,   // minimum width of sub-menus in em units
	maxWidth: 13,   // maximum width of sub-menus in em units
	extraWidth: 1,     // extra width can ensure lines don't sometimes turn over due to slight rounding differences and font-family
	//printf("\t\t\dropShadows: false,\n");
	autoArrows: true // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance\n");
	}).superfish();
	}); 
	</script>
	<script src='http://www.blueletterbible.org/scripts/blbToolTip/BLB_ScriptTagger-min.js'type='text/javascript'></script>
	<script type='text/javascript'>
		BLB.Tagger.Translation = 'NKJV';
		BLB.Tagger.HyperLinks = 'all'; // 'all', 'none', 'hover'
		BLB.Tagger.HideVersions = false;
		BLB.Tagger.TargetNewWindow = true;
		BLB.Tagger.Style = 'par'; // 'line' or 'par'
	</script>
	<?echo $more;?>
	</head>
	<? 
}

function sf_menu_element($nav, $t, $e) {
	printf("\t<li class='nav' >\n");
	
  if (is_null($e)) {
    if (strpos($t[1], "ttp://") || strpos($t[1], ".pdf")) {
      printf("\t\t\t<a class='nav%d' href='%s' target='_blank'>%s</a>n", $nav, $t[1], $t[0]);
    } else {
      printf("\t\t\t<a class='nav%d' href='%s'>%s</a>\n", $nav, $t[1], $t[0]);
    }
    printf("\t</li>\n");
    return;
  }

  printf("\t\t<a class='nav%d' href='#'>%s</a>\n", $nav, $t[0]);
  printf("\t\t<ul class='subnav'>\n");
  for ($i = 0; $i < sizeof($e); $i++) {
	  	printf("\t\t\t<li>\n");
		if (strpos($e[$i][1], "ttp://") || strpos($e[$i][1], ".pdf")) {
	    printf("\t\t\t\t<a class='menu' href='%s' target='_blank'>%s</a>\n", $e[$i][1], $e[$i][0]);
		} else {
	    printf("\t\t\t\t<a class='menu' href='%s'>%s</a>\n", $e[$i][1], $e[$i][0]);
		}
		printf("\t\t\t</li>\n");
  }
	printf("\t\t</ul>\n");
	printf("\t</li>\n");
}

function sf_menu() {
	printf("<ul class='sf-menu' id='nav'>\n");

	$title = array(
		array("About", "/staff.php"),
		array("Gatherings", "/events.php"),
		array("Ministries", "/ministries.php"),
		array("Resources", "/resources.php"),
		array("Teaching", "/sermon.php"),
		array("Contact", "/contact.php"),
		array("Giving", "/donations.php")
		);
  
	$elements = array(
		array(array("Beliefs","/beliefs.php"),array("History","/history.php"),array("Staff","/staff.php"),array("Leadership","/leadership.php")),
		array(array("Services","/services.php"),array("Events","/events.php"),array("Home Fellowships","/homefellowship.php"),array("Church Calendar","/church_calendar.php"), array("Veritas Christian Fellowship","http://veritaschristianfellowship.org")),  
		array(array("Men's","/ministry/men"),array("Women's","/ministry/women"),array("High School","/ministry/24seven"),array("Junior High","/ministry/abide"),array("Children's","/ministry/cm"), array("Casa De Bendicion","http://thehouseofblessing.org"), array("Missionaries","/missionaries.php"), array("More","/ministries.php")),
		array(array("Bookstore","/ministry/dp"),array("Office Library","/officelibrary"),array("Church Needs","/needs.php"),array("Bible in a Year","/Bible1year.php"),array("Facebook","http://facebook.com/southhillcalvary/"),array("Church Blog","http://blog.southhillcalvary.org"),array("Pastor's Blog","http://pastorronsanchez.com"),array("Worship Album","http://southhillcalvary.bandcamp.com")),	
		array(array("Current Sermons","/current_sermons.php"),array("Archived Sermons","/sermon.php"),array("Sermon Series","/sermons_series.php"),array("Live Stream","/video_live.php"),array("Independent Study","/independentstudy.php"),array("Podcasts","/podcast.php")),
		array(array("Contacts","/contact.php"),array("Church Directory", "/directory"),array("Directions","/direction.php"),array("Prayer Requests","/prayer_request.php")),
		NULL
		);
	         

	for ($i=0; $i < count ($title); $i++) {
		sf_menu_element($i+1, $title[$i], $elements[$i]);
	}
	printf("</ul>\n");
}



function CheckTime($currentTime, $startTime, $endTime){
	// written 11/26/2006 by Patrick H. (patrickh@gmail.com)
	//
	// the time passed must meet all the below criteria to return 1 (true):
	//
	// - current hour needs to be equal or greater than start hour
	// - current hour needs to be equal or less than end hour
	// - current minute needs to be equal or greater than start minute (if current hour is ok)
	// - current minute needs to be equal or less than end minute (if current hour is ok)
	//
	// if any of those checks does not pass, it will return 0 (false)

	global $cHour;
	global $cMin;
	global $sHour;
	global $sMin;
	global $eHour;
	global $eMin;

	// break up current time
	$now = explode(":",$currentTime);
	$cHour = intval($now[0]);	// current time - hour
	$cMin = intval($now[1]);	// current time - minute

	// break up start time
	$start = explode(":",$startTime);
	$sHour = intval($start[0]);	// start of range - hour
	$sMin = intval($start[1]);	// start of range - minute

	// brek up end time
	$end = explode(":",$endTime);
	$eHour = intval($end[0]);	// end of range - hour
	$eMin = intval($end[1]);	// end of range - minute

	// this is the variable used to track the result of the checks
	$pass = true;

	if($sHour <= $eHour){
		// the range is on the same day

		// compare to the start hour
		if($cHour < $sHour){
			$pass = false;
		};

		// compare to the end hour
		if($cHour > $eHour){
			$pass = false;
		};

		// compare to the start min
		if($cHour == $sHour){
			if($cMin < $sMin){
				$pass = false;
			};
		};

		// compare to the end min
		if($cHour == $eHour){
			if($cMin > $eMin){
				$pass = false;
			};
		};

	} else {
		// the range is overnight, so the logic is a little different

		if( ($cHour < $sHour) && ($cHour > $eHour) ){
			$pass = false;
		};

		// compare to the start min
		if($cHour == $sHour){
			if($cMin < $sMin){
				$pass = false;
			};
		};

		// compare to the end min
		if($cHour == $eHour){
			if($cMin > $eMin){
				$pass = false;
			};
		};

	};

	// done with check, return the result
	if($pass == false){
		return 0;	// failed
	} else {
		return 1;	// passed
	};

};


function live_video() {
	$now = time();
	$day = date('w', $now);
	$currentTime = date('H:i',$now);
	
	$WedStart="18:45";
	$WedEnd="20:30";
	$SunStart="10:00";
	$SunEnd="11:30";
	
	if (($day == 0 && CheckTime($currentTime,$SunStart,$SunEnd)) ||
	    ($day == 3 && CheckTime($currentTime,$WedStart,$WedEnd))) {
		printf("<div id='banner'>\n");
		
		
		if ($day == 0) {
			$banner = "Sunday Morning Service - 10:00 to 11:30AM";
		} else if ($day == 3) {
			$banner = "Wednesday Evening Service - 7:00 to 8:30PM";
		}
		printf("<a href='./video_live.php'>View Live Video in Progress:  %s</a>\n", $banner);
		printf("</div></br>\n");
	}
}

function begin_body() {
printf("<body>\n");
printf("\t<div class='body'>\n");
printf("\t\t<div class='header' id='header'>\n");
printf("\t\t\t<a href='/'><img src='/images/header.png' alt='South Hill Calvary Chapel'/></a>\n");
printf("\t\t</div>\n");
printf("\t\t<div class='menubar'>\n");
sf_menu();
printf("\t\t</div>\n");
}

function page_title($title) {
	printf("\t\t<div class='clear'>&nbsp;</div>\n");
	printf("\t\t\t<h2 class='page_title'>%s</h2></br>\n", $title);
	printf("\t\t<div class='content'>\n");
}

function page_title_no_content($title) {
	printf("\t\t<div class='clear'>&nbsp;</div>\n");
	printf("\t\t\t<h2 class='page_title'>$title</h2></br>\n");
}

function end_page_title() {
	printf("\t\t\t</div>\n");
}

function end_body() {

printf("\t\t<div id='footer'>\n");
$now = time();
$YEAR = date('Y',$now);
printf("\t\t\t\tCopyright &copy; 2004-%s South Hill Calvary Chapel - Puyallup, WA. All rights reserved.\n", $YEAR);
printf("&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;<a id='footer' href='mailto:webservant@southhillcalvary.org'>Contact the Webservant</a>\n");
printf("\t\t</div>\n");

printf("\t</div>\n");

//Google Analytics
printf("<script type=\"text/javascript\">\n");
printf("\tvar gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");\n");
printf("\tdocument.write(unescape(\"%%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%%3E%%3C/script%%3E\"));\n");
printf("</script>\n");
printf("<script type=\"text/javascript\">\n");
printf("\ttry {\n");
printf("\tvar pageTracker = _gat._getTracker(\"UA-7784650-2\");\n");
printf("\tpageTracker._trackPageview();\n");
printf("\t} catch(err){}\n");
printf("</script>\n");

printf("</body>\n");
printf("</html>\n");
}


function find_schedules ($name) {

	$table = "schedules";
	connect_select_test($table);

	$query="SELECT * FROM $table WHERE ministry='$name' ORDER BY start_date DESC";
	$result=mysql_query($query) or die (mysql_error());
	$num_rows=mysql_num_rows($result);

	$now = time();

	for ($i = 0; $i < $num_rows; $i++) {
		$end_date = mysql_result($result,$i,"end_date");
		$php_end_date = strtotime($end_date);

		if ($php_end_date < $now) {
			break;
		}


		$start_date = mysql_result($result,$i,"start_date");
		$php_start_date = strtotime($start_date);
		$start_year = date("Y", $php_start_date);
		$start_month = date("F", $php_start_date);

		$php_end_date = $php_end_date - (24 * 60 * 60); //go back one day
		$end_year = date("Y", $php_end_date);
		$end_month = date("F", $php_end_date);

		$LINK = mysql_result($result,$i,"link");
		if ($start_month == $end_month) {
			$DATE = $start_month." ".$start_year;
		} else if ($start_year != $end_year) {
			$DATE = $start_month." ".$start_year."-".$end_month." ".$end_year;
		} else {
			$DATE = $start_month."-".$end_month." ".$end_year;
		}
		printf("<a href='%s' target='_blank'>%s</a>, ", $LINK, $DATE);
	} 
}


function picture_caption ($pic, $width, $height, $caption) {
	printf("<div>\n");
	if ($width > 0 && $height > 0) {
		printf("\t<img src='%s' width='%dpx' height='%dpx'/>\n", $pic, $width, $height);
	} else if ($width > 0) {
		printf("\t<img src='%s' width='%dpx'/>\n", $pic, $width);
	} else if ($height > 0) {
		printf("\t<img src='%s' height='%dpx'/>\n", $pic, $height);
	} else {
		printf("\t<img src='%s'/>\n", $pic);
	}
	printf("\t<H4 class='caption'>\n");
	printf("\t\t%s\n", $caption);
	printf("\t</H4>\n");
	printf("</div>\n");
}

function media_slideshow_recent ($tag) {

printf("<script type='text/javascript'>\n");
//AJAX Feed API Picasa Slideshow    
printf("function LoadSlideShow() {\n");
printf("\tvar feed  = 'https://picasaweb.google.com/data/feed/base/user/southhillcalvary?alt=rss&kind=photo&tag=%s&filter=1&authkey=CObjPTvgbng&hl=en_US';\n", $tag);
printf("\tvar options = {\n");
printf("\t\tdisplayTime:2000,\n");
printf("\t\ttransistionTime:600,\n");
printf("\t\tscaleImages:false,\n");
printf("\t\tmaintainAspectRatio: true,\n");
printf("\t\tlinkTarget: google.feeds.LINK_TARGET_BLANK,\n");
printf("\t\tfullControlPanel: true,\n");
printf("\t\tfullControlPanelSmallIcons: true,\n");
printf("\t\tfullControlPanelCursor: true,\n");
printf("\t\tpauseOnHover: false\n");
printf("\t};\n");
printf("\tvar ss = new GFslideShow(feed, 'picasaSlideshow', options);\n");
printf("}\n");
/*
* Use google.load() to load the AJAX Feeds API
* Use google.setOnLoadCallback() to call LoadSlideShow once the page loads
*/
printf("google.load('feeds', '1');\n");
printf("google.setOnLoadCallback(LoadSlideShow);\n");

printf("</script>\n");
printf("\t\t\t<div id='picasaSlideshow' class='gslideshow'><div class='feed-loading'>Loading...</div></div>\n");
}

function row_class ($x) {

   if (($x % 2) == 1) {
      $row = 'row_odd';
    } else {
      $row = 'row_even';
    }
   return $row;
}

/*
################## IN PAGE LINKS ###################
*/
function in_page_link($num_links, $image, $linkList, $textList, $imageList) {

  printf("\t\t\t\t\t<div class='floatright'><strong>\n");

  for ($i = 0; $i < $num_links; $i++) {
    if ($image == true) {
      printf("\t\t\t\t\t\t<a href='%s'><img src='%s' border='0'></a>\n", 
        $linkList[$i], $imageList[$i]);
      printf("\t\t\t\t\t\t<a href='%s'>%s</a>", $linkList[$i],$textList[$i]);
    } else {
      printf("\t\t\t\t\t\t<a href='%s'>%s</a>", $linkList[$i],$textList[$i]);
    }
    if ($i == $num_links-1) {
      printf("\n");
    } else {
      printf(",\n");
    }
  } 
  printf("\t\t\t\t\t</strong></div>\n");
  printf("\t\t\t\t\t<div class='clear'>&nbsp;</div>\n");
}

/*
################### TABLES ####################3
*/

function table_header ($header, $width) {
  $len = sizeof($header);

  printf("<TABLE>\n");
  printf("\t<tr>\n");
  for ($i = 0; $i < $len; $i++) {
	  printf("\t\t<td width='%dpx'><h5>%s</h5></td>\n", $width[$i], $header[$i]);
	}
  printf("\t</tr>\n");
  
}

function table_row ($row, $row_class, $id) {
	$len = sizeof($row);
	if (empty($id)) {
		printf("\t<tr class='dotted_lines %s'>\n", $row_class);
	} else {
		printf("\t<tr class='dotted_lines %s' id='%s'>\n", $row_class,  htmlentities($id));
	}
	for ($i = 0; $i < $len; $i++) {
		printf("\t\t<td>%s</td>\n", $row[$i] );
	}
	printf("\t</tr>\n");
}


function table_footer ($cols) {
  printf("\t<tr class='dotted_lines'><td colspan=%d>&nbsp</td></tr>\n", $cols);
  printf("</TABLE>\n");
}

//Print Contact Table
function contact_table ($result, $num_rows, $title) {

  printf("<TABLE>\n");
  printf("\t<tr>\n");
  printf("\t\t<td colspan='4'><h4>%s</h4></td>\n", $title);
  printf("\t</tr>\n");

  for ($i=0; $i < $num_rows; $i++) {
    $title=mysql_result($result,$i,"position");
    $first=mysql_result($result,$i,"first");
	$tag=mysql_result($result,$i,"tag");
    $last=mysql_result($result,$i,"last");
    $phone=mysql_result($result,$i,"phone");
    $email=mysql_result($result,$i,"email");

    $cell1=$title;
    $cell2=add_title($first." ".$last);
    $cell3=$phone;
    $cell4="<a href='mailto:".$email."'>".$email."</a>";
    $row = array($cell1,$cell2,$cell3,$cell4);
    table_row($row, row_class($i+1), $tag);
  }
  table_footer(4);
}

//Print Wed CM Table
function cm_wed_table ($result, $num_rows) {

  $classes = array("","4yrs-5th Grade","Walkers-3yrs");
  $header = array("Date","Class","Verse","Title");
  table_header($header, NULL);

  $i=0;
  while ($i < $num_rows) {
    $date=mysql_result($result,$i,"date");
    $php_time = strtotime($date);
    $xml_date = date("F jS, Y", $php_time);

    $book=mysql_result($result,$i,"book");
    $verse=mysql_result($result,$i,"verse");
    $title=mysql_result($result,$i,"title");
    $link=mysql_result($result,$i,"link");
    $class=mysql_result($result,$i,"class");

    $cell2=$classes[$class];
    $cell3=$book." ".$verse;
    $cell4="<a href='".$link."' target='_blank'>".$title."</a>";
    $row = array($xml_date, $cell2,$cell3,$cell4); 
    table_row($row, row_class($i+1), NULL);

    $i++;
  }
  table_footer(sizeof($row));
}


function cm_pc_table ($result, $num_rows, $current, $day) {

  if ($day == 'Sunday') {
    $classes = array("","Nursery","Walkers", "2&3 Year Olds", "4yrs-K",
                   "1st-5th Grade");
    $header = array("Class","Service","Month","Title");
  } else {
    $classes = array("","2-4 Year Olds", "4yrs-5th Grade");
    $header = array("Class","Month","Title");
  }

  $now = time();

  table_header($header, NULL);

  $i=0;
  while ($i < $num_rows) {

    $date=mysql_result($result,$i,"date");
    $php_date = strtotime($date);
    $current_year = date("Y", $now);
    $year = date("Y", $php_date);
    if ($current && $current_year != $year) {
      $i++;
      continue;
    }
    $current_month = date("F", $now);
    $month = date("F", $php_date);
    if ($current && $current_month != $month) {
      $i++;
      continue;
    }

    $service=mysql_result($result,$i,"service");
    if ($service == 1) {$service = "1st";}
    else if ($service == 2) {$service = "2nd";}
    else {$service = "All services";}

    $class=mysql_result($result,$i,"class");
    $link=mysql_result($result,$i,"link");
    $title=mysql_result($result,$i,"title");

    $cell1=$classes[$class];
    $cell2=$service;
    $cell3=$month;
    if (strpos($link, "docs.google.com")) {
      $cell4="<a href='".$link."' target='_blank'>".$title."</a>";
    } else {
      $cell4="<a href='http://southhillcalvary.org".$link."' target='_blank'>".$title."</a>";
    }
    if ($day == 'Sunday') {
      $row = array($cell1, $cell2,$cell3, $cell4);
    } else {
      $row = array($cell1, $cell3, $cell4);
    }
    table_row($row, row_class($i+1), NULL);

    $i++;
  }
  table_footer(sizeof($row));
}

function right_bar_open ($tag, $facebook) 
{
	printf("<div class='rightbar'>\n");
	if (!empty($tag)) {
		media_slideshow_recent($tag);
		printf("<br/>\n");
	}
	
	if (!empty($facebook)) {
		printf("<div class='paragraph'>\n");
		printf("<a href='%s' target='_blank'><img src='/images/facebook_35x35.png' width='20' height='20'> Facebook</a>\n", $facebook);
		printf("</div>\n");
	}
}
function right_bar_close () 
{
	printf("</div>\n");
}
function ministry_footer ($leader, $blessing) 
{
	printf("\t\t\t<div>\n");
	printf("\t\t\t\t<br/><h4>Ministry Contact</h4>\n");
	printf("\t\t\t\t%s<br/>\n", add_title($leader[0]));
	printf("\t\t\t\t%s<br/>\n", $leader[1]);
	printf("\t\t\t\t<a href='mailto:%s'>%s</a><br/><br/>\n", $leader[2], $leader[2]);
	printf("\t\t\t\t<strong><em>%s</em></strong>\n", $blessing);
	printf("\t\t\t</div>\n");
	printf("<div class='clear'>&nbsp;</div>\n");
}
function ministry_header ($verse) 
{
	printf("\t\t\t<div class='paragraph'>\n");
	printf("\t\t\t\t<strong><em>\"%s\"</em></strong> %s\n",$verse[0], $verse[1]);
	printf("\t\t\t</div>\n");
}

//Print Ministry Event Table
function ministry_event_table ($result, $num_rows) {
	global $border;
	global $max_width;

	$now = time();
	$go = false;
	for ($i=0; $i < $num_rows; $i++) {
		$date=mysql_result($result,$i,"start");
		$php_start_time = strtotime($date);

		if ($now > $php_start_time) {
			continue;
		}
		$go = true;
		$break;
	}
	if (!$go) {
		return;
	}

	printf("<h4>Events</h4>\n");
	for ($i=0; $i < $num_rows; $i++) {
		$date=mysql_result($result,$i,"start");
		$php_start_time = strtotime($date);

		if ($now > $php_start_time) {
			continue;
		}
		$xml_date = date("m/d/y", $php_start_time);
		$title=mysql_result($result,$i,"title");
		$id=mysql_result($result,$i,"id");
		$picture=mysql_result($result,$i,"picture");
		$no_start=mysql_result($result,$i,"no_start");

		printf("<div>\n");   
		if (empty($picture)) {
			printf("\t<a href='/events.php#%s'>\n", $id);
		} else {
			printf("\t<a href='/event/%s'>\n", $id);
		}
	
		if ($no_start) {
			printf("%s</a>\n", $title);
		} else {
			printf("%s -- %s</a>\n", $xml_date, $title);
		}
		printf("</div>\n");
	}
}

//Print Ministry Resource Table
function ministry_resource_table ($resource, $title) {
  global $border;
  global $max_width;

  printf("<H4>%s</H4>\n", $title);

  $num_rows = sizeof($resource);
  printf("<ul>\n");
  for ($i=0; $i < $num_rows; $i++) {
	printf("\t<li>\n");
    if ($resource[$i][0] == "") {
      printf("\t\t%s\n", $resource[$i][1]);
    } else if (strpos($resource[$i][0], "ttp://") ||
               strpos($resource[$i][0], ".pdf")) {
      printf("\t\t<a target='_blank' href='%s'>%s</a>\n",
        $resource[$i][0], $resource[$i][1]);
    } else {
      printf("\t\t<a href='%s'>%s</a>\n",
        $resource[$i][0], $resource[$i][1]);
    }
	printf("\t</li>\n");
  }
  printf("</ul>\n");
}

//Print Event Table
function events_table ($result, $num_rows) {
	global $border;
	global $max_width;

	$now = time();

	$i=0;
	printf("<div id='plainlist'>\n");
	printf("<ul>\n");
	while ($i < $num_rows) {

		$date=mysql_result($result,$i,"start");
		$no_start=mysql_result($result,$i,"no_start");
		$php_start_time = strtotime($date);

		$date=mysql_result($result,$i,"end");
		$no_end=mysql_result($result,$i,"no_end");
		$php_end_time = strtotime($date);

		$php_end_day = date("z", $php_end_time);
		$php_start_day = date("z",$php_start_time);

		if ($now > $php_end_time) {
			$i++;
			continue;
		}

		if ($no_start) {
			$duration = 0;
		} else if ($no_end) {
			$date1 = date("l, F jS, g:i a", $php_start_time);
			$duration = $date1;
		} else if ($php_end_day != $php_start_day) {
			$date1 = date("l, F jS, g:i a", $php_start_time);
			$date2 = date("l, F jS", $php_end_time);
			$duration = $date1 . " - " . $date2;
		} else {
			$date1 = date("l, F jS, g:i a", $php_start_time);
			$date2 = date("g:i a", $php_end_time);
			$duration = $date1 . " - " . $date2;
		}
		$duration = strtoupper($duration);

		$title=strtoupper(mysql_result($result,$i,"title"));
		$announcement=mysql_result($result,$i,"announcement");
		$ministry=mysql_result($result,$i,"ministry");
		$flyer=mysql_result($result,$i,"flyer");
		$directions=mysql_result($result,$i,"directions");
		$id=mysql_result($result,$i,"id");
		$picture=mysql_result($result,$i,"picture");
    
		printf("<li class='event'>\n");
		if ($no_start) {
			printf("\t\t<H5>$title<a name='$id'></a></H5>\n");
		} else {
			printf("\t\t<H5> $title - $duration<a name='$id'></a></H5>\n");
		}
		printf("\t\t<div class='paragraph'>\n");
		printf("\t\t\t%s\n", $announcement);
		if (!empty($picture)) {
			printf("\t\t\t<a href='/event/%s'>[MORE DETAILS]</a>\n", $id);
		}
		else if (!empty($flyer) || !empty($directions)) {
			if (!empty($flyer)) {
				printf("<a href='%s' target='_blank'> [SEE FLYER] </a>\n",$flyer);
			}
			if (!empty($directions)) {
				printf("<a href='%s' target='_blank'> [DIRECTIONS] </a>\n",$directions);
			}
		}
		printf("\t\t\t</div>\n");
		printf("\t\t</li>\n");
		$i++;
	}
	printf("</ul>\n");
	printf("</div>\n");
}

// Print Sermon Series Table
function sermon_series_table ($result, $num_rows) {
  global $border;
  global $max_width;

  $header = array("Date","Series Title","Speaker","Display");
  $width = array(80, 490, 270, 40);
  table_header($header, $width);

  for ($i=0; $i<$num_rows; $i++) {

    $date=mysql_result($result,$i,"date");
    $php_date = strtotime($date);
    $date_output = date("m/d/y", $php_date);

    $title=mysql_result($result,$i,"title");
    $first=mysql_result($result,$i,"first");
    $last=mysql_result($result,$i,"last");
    $id=mysql_result($result,$i,"id");
    $table=mysql_result($result,$i,"table");

    $cell1=$date_output;
    $cell2=$title;
    $cell3=add_title($first." ".$last);
	$cell4="<a href='./sermons_series.php?id=".$id."&table=".$table."&title=".$title."'><img src='/images/play_arrow.png' alt='play'/></a>";
    $row = array($cell1, $cell2,$cell3,$cell4);
    table_row($row, row_class($i+1), NULL);
  }
  table_footer(sizeof($row));
}

//Print Sermon Table
function sermon_table ($result, $num_rows,$table) {
	global $border;
	global $max_width;

  $header = array("Date","Scripture","Title","Speaker","Play");
  $width = array(80, 150, 450, 170, 30);
  table_header($header, $width);

  for ($i=0; $i<$num_rows; $i++) {
    $date=mysql_result($result,$i,"date");
    $php_time = strtotime($date);
    $xml_date = date("m/d/y", $php_time);

    $id=mysql_result($result,$i,"id");
    $book=mysql_result($result,$i,"book");
    $verse=mysql_result($result,$i,"verse");
    $name=mysql_result($result,$i,"name");
    $title=mysql_result($result,$i,"title");

    $cell1=$xml_date;
    $cell2=$book." ".$verse;
    $cell3=$title;
    $cell4=add_title($name);
    $cell5="<a href='./sermon/".$id."'><img src='/images/play_arrow.png' alt='play'/></a>";

    $row = array($cell1, $cell2,$cell3, $cell4, $cell5);
    table_row($row, row_class($i+1), NULL);
  }
  table_footer(5);
}

//Print Sermon Table
function sermon_snippet ($result, $num_rows,$table) {

  for ($i=0; $i<$num_rows; $i++) {
    $date=mysql_result($result,$i,"date");
    $php_time = strtotime($date);
    $xml_date = date("D, M jS, Y", $php_time);

    $id=mysql_result($result,$i,"id");
    $book=mysql_result($result,$i,"book");
    $verse=mysql_result($result,$i,"verse");
    $title=mysql_result($result,$i,"title");

    $ref=$book." ".$verse;
		printf("<div class='blocks'>\n");
		printf("<a class='title' href='./sermon/%d'><h5>%s</h5></a>\n", $id, $title);
		printf("%s <a href='./sermon/%d'>on %s</a>\n", 
		   $ref, $id, $xml_date);
		printf("</div>\n");

  }
}

function check_email_address($email) {
  // First, we check that there's one @ symbol, 
  // and that the lengths are right.
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters 
    // in one section or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
?'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
      return false;
    }
  }
  // Check if domain is IP. If not, 
  // it should be valid domain name
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
?([A-Za-z0-9]+))$",
$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}

function add_title($name) {
	$p = 'Pastor';
	$e = 'Elder';
	$m = 'Missionary';
	$c = 'Chaplain';
	$patterns = array("/Ron Sanchez/", "/Jason Sanchez/", "/Alan Chappell/", "/Phil Spagnolo/", "/Bob Caldwell/", "/Kevin Day/", "/Frank Boulet/", "/Kris Repp/", "/Joel Meyer/", "/Samy Tanagho/", "/Luis Vargas/", "/Fikret Bocek/", "/Ed Williams/", "/Charles Gillaspie/", "/Jason Murdock/", "/Brian Headrick/");
	$replacements = array($p." Ron Sanchez", $p." Jason Sanchez", $p." Alan Chappell", $p." Phil Spagnolo", $p." Bob Caldwell", $p." Kevin Day", $c." Frank Boulet", $m." Kris Repp", $m." Joel Meyer", $m." Samy Tanagho", $m." Luis Vargas", $m." Fikret Bocek", $e." Ed Williams", $p." Charles Gillaspie", $p." Jason Murdock", $p." Brian Headrick");
	
	return preg_replace($patterns, $replacements, $name);
}

function isApple() {

$agent = $_SERVER['HTTP_USER_AGENT'];

$var[0] = 'iPhone';
$var[1] = 'iPad';
$var[2] = 'iPod';
$var[3] = 'Macintosh';

$result = count($var);

		for ($i=0;$i<$result;$i++){  
		  
			$ausg = stristr($agent, $var[$i]);  
			if(strlen($ausg)>0){
				
				return TRUE;
				exit;
				
			}
		}

}

function printNeedsLogin($page, $vars){
	
	printf("<form style='width:%s; margin-left:%s;' action='%s'>\n", "50%", "33%", $page);
	
	printf("\t<input type='text' name='user' placeholder='User Name' value='%s' />\n", $_GET['user']);
	
	printf("\t<input type='password' name='pass' placeholder='Password' value='%s' />\n", $_GET['pass']);
	
	if (!empty($vars)){
		
		$numVars = count($vars);
		
		for ($i=0;$i<$numVars;$i=$i+2){
			
			printf("\t<input type='hidden' name='%s' value='%s' />\n", $vars[$i], $vars[$i+1]);
		}
	}
	
	printf("\t<input type='submit' value='Login' />\n");
	
	printf("</form>\n");
	
	if ($_GET['user'] && $_GET['pass']){
		
		if ($_GET['user'] == "shcc" && $_GET['pass'] == "Fam1ly"){
			
			printf("<script type='text/javascript'>document.cookie= \"needsLogin=1\"; window.location = \"%s\";</script>", $page);
		}
		else{
			
			printf("<font style='color:red;'>Incorrect User Name or Password.</font>\n");
		}
	}
}
/*
############## ARRAY OF BIBLE BOOKS #####################
*/

$bible_books = array(
array("",0),
array("Genesis",1),
array("Exodus",1),
array("Leviticus",1),
array("Numbers",1),
array("Deuteronomy",1),
array("Joshua",1),
array("Judges",1),
array("Ruth",1),
array("1 Samuel",1),
array("2 Samuel",1),
array("1 Kings",1),
array("2 Kings",1),
array("1 Chronicles",1),
array("2 Chronicles",1),
array("Ezra",1),
array("Nehemiah",1),
array("Esther",1),
array("Job",1),
array("Psalms",1),
array("Proverbs",1),
array("Ecclesiastes",1),
array("Song of Solomon",1),
array("Isaiah",1),
array("Jeremiah",1),
array("Lamentations",0),
array("Ezekiel",0),
array("Daniel",0),
array("Hosea",0),
array("Joel",0),
array("Amos",0),
array("Obadiah",0),
array("Jonah",0),
array("Micah",0),
array("Nahum",0),
array("Habakkuk",0),
array("Zephaniah",0),
array("Haggai",0),
array("Zechariah",0),
array("Malachi",0),
array("Matthew",1),
array("Mark",1),
array("Luke",1),
array("John",1),
array("Acts",1),
array("Romans",1),
array("1 Corinthians",1),
array("2 Corinthians",1),
array("Galatians",1),
array("Ephesians",1),
array("Philippians",1),
array("Colossians",1),
array("1 Thessalonians",1),
array("2 Thessalonians",1),
array("1 Timothy",1),
array("2 Timothy",1),
array("Titus",1),
array("Philemon",1),
array("Hebrews",1),
array("James",1),
array("1 Peter",1),
array("2 Peter",1),
array("1 John",1),
array("2 John",1),
array("3 John",1),
array("Jude",1),
array("Revelation",1)
);
?>
